package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.epg.EpgActualSearchMapper;
import com.ztgf.shopping.models.bo.epg.EpgActualSearchSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgActualSearchDTO;
import com.ztgf.shopping.models.service.epg.IEpgActualSearchService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;

import javax.annotation.Resource;

/**
 * epg_actual_search控制器
 *
 * @author YUE
 * @Date 2020-09-22 17:10:54
 */
@DubboService
public class EpgActualSearchServiceImpl extends ServiceImpl<EpgActualSearchMapper, EpgActualSearchDTO> implements IEpgActualSearchService {

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public boolean add(EpgActualSearchSaveBO saveBO) {
        EpgActualSearchDTO dto = new EpgActualSearchDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    /**
     * 添加或者更新实施搜索词语
     *
     * @param searchTag
     */
    @Async
    public void addOrUpdateNum(String searchTag) {
        EpgActualSearchDTO epgActualSearchDTO = getBySearchWord(searchTag);
        if (null == epgActualSearchDTO) {//保存
            EpgActualSearchSaveBO epgActualSearchSaveBO = new EpgActualSearchSaveBO();
            epgActualSearchSaveBO.setSearchWord(searchTag);
            epgActualSearchSaveBO.setSearchNum(1);//默认1次
            add(epgActualSearchSaveBO);
        } else {//修改次数
            epgActualSearchDTO.setSearchNum(epgActualSearchDTO.getSearchNum() + 1);
            updateById(epgActualSearchDTO);
        }
    }

    /**
     * 根据关键词 查找
     *
     * @param searchTag
     * @return
     */
    public EpgActualSearchDTO getBySearchWord(String searchTag) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("search_word", searchTag);
        return selectOne(wrapper);
    }
}